package com.rerun.phone.base;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.entity.Dictionaries;
import com.rerun.entity.District;
import com.rerun.entity.Province;
import com.rerun.service.IDictionariesService;
import com.rerun.service.ProvinceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * The type Public controller.
 */
@Api(value = "公用", description = "公用")
@RestController
@RequestMapping(value = "/baseApi")
public class PublicController {
    /**
     * The Dictionaries service.
     */
    @Autowired
    IDictionariesService dictionariesService;
    /**
     * The Province service.
     */
    @Autowired
    ProvinceService provinceService;

    /**
     * 查询级联字典数据
     *
     * @param name the name
     * @return dictionary
     */
    @ApiOperation(value = "根据CodeName获取字典", httpMethod = "GET", notes = "根据CodeName获取字典")
    @RequestMapping(value = "/dictionary/{name}")
    @ResponseExceptionReturn
    public Rest getDictionary(@PathVariable String name) {

        try {
            List<List<Dictionaries>> dictionaries = dictionariesService.selectDictionariesByCode(name);
            return Rest.okData(dictionaries);
        } catch (Exception e) {
            e.printStackTrace();
            return Rest.failure("查询出错");
        }
    }

    /**
     * 根据城市名获取其下面地区
     *
     * @param cityName the city name
     * @return district
     */
    @ApiOperation(value = "根据城市名获取其下面地区", httpMethod = "GET", notes = "根据城市名获取其下面地区")
    @RequestMapping(value = "/getDistrict/{cityName}")
    @ResponseExceptionReturn
    public Rest getDistrict(@ApiParam(required = true, name = "cityName", value = "城市名 eg郑州市") @PathVariable String cityName) {
        List<District> districts = provinceService.getListDistrictByCityName(cityName);
        return Rest.okData(districts);
    }

    /**
     * Gets city list.
     *
     * @param provinceName the province name
     * @return the city list
     */
    @ApiOperation(value = "根据省份名获取其下所有市", httpMethod = "GET", notes = "根据省份名获取其下所有市")
    @RequestMapping(value = "/getCity/{provinceName}")
    @ResponseExceptionReturn
    public Rest getCityList(@ApiParam(required = true, name = "provinceName", value = "省份名 eg河南省") @PathVariable String provinceName) {
        List<District> districts = provinceService.getProvinceListByPname(provinceName);
        return Rest.okData(districts);
    }

    /**
     * Gets province list.
     *
     * @return the province list
     */
    @ApiOperation(value = "获取所有省份", httpMethod = "GET", notes = "获取所有省份")
    @RequestMapping(value = "/getProvinces")
    @ResponseExceptionReturn
    public Rest getProvinceList() {
        EntityWrapper<Province> wrapper = new EntityWrapper<>();
        List<Province> provices = provinceService.selectList(wrapper);
        return Rest.okData(provices);
    }

    @ApiOperation(value = "扫描二维码下载APP 非接口", httpMethod = "GET", notes = "扫描二维码下载APP")
    @RequestMapping(value = "/app/download", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public ModelAndView qrCode() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("system/qrCode/index");
        return modelAndView;
    }
}
